import React, { useReducer } from 'react'

export default function ReducerCom() {
  let [state,dispatch]=useReducer((state,action)=>{
    switch (action.type) {
      case 'add':
        return state + action.payload
      case 'sub':
        return state - action.payload
      case 'chen':
        return state * action.payload
      case 'chu':
        return state / action.payload
      case 'quyu':
        return state % action.payload
        
    
      default:
        return state
    }
  },5)
  return (
    <div>
      <h2>{state}</h2>
      <button onClick={()=>{
        dispatch({type:'add',payload:10})
      }}>增加</button>
      <button onClick={()=>{
        dispatch({type:'sub',payload:6})
      }}>减少</button>
      <button onClick={()=>{
        dispatch({type:'chen',payload:4})
      }}>相乘</button>
      <button onClick={()=>{
        dispatch({type:'chu',payload:2})
      }}>相除</button>
      <button onClick={()=>{
        dispatch({type:'quyu',payload:6})
      }}>取余</button>
    </div>
  )
}
